// SimpleImageButton.cpp : implementation file
//

#include "stdafx.h"
#include "CupLogin.h"
#include "SimpleImageButton.h"
#include "CommonFounction.h"

using namespace std;
using namespace Gdiplus;
// CSimpleImageButton

IMPLEMENT_DYNAMIC(CSimpleImageButton, CButton)

CSimpleImageButton::CSimpleImageButton()
:m_pNormalImage(NULL)
,m_pHorverImage(NULL)
,m_pPressImage(NULL)
,m_eState(enmButtonState_Normal)
, m_eventListener(NULL)
{
	m_buttonStyle = WS_VISIBLE|WS_CLIPSIBLINGS|WS_CLIPCHILDREN|BS_OWNERDRAW;
}

CSimpleImageButton::~CSimpleImageButton()
{
}


BEGIN_MESSAGE_MAP(CSimpleImageButton, CButton)
	ON_WM_ERASEBKGND()
	ON_WM_MOUSELEAVE()
	ON_WM_MOUSEHOVER()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_MOUSEMOVE()
END_MESSAGE_MAP()



// CSimpleImageButton message handlers

void CSimpleImageButton::CreateButton(UINT nID , LPCTSTR lpszCaption, wstring normal, wstring horver, wstring press, wstring disEnable, CWnd* pParentWnd)
{	
	USES_CONVERSION;	
	GetGamePath(normal);
	GetGamePath(horver);
	GetGamePath(press);
	GetGamePath(disEnable);
	m_pNormalImage = Gdiplus::Bitmap::FromFile(normal.c_str());
	m_pHorverImage = Gdiplus::Bitmap::FromFile(horver.c_str());
	m_pPressImage = Gdiplus::Bitmap::FromFile(press.c_str());
	if (m_pNormalImage)
	{
		RECT rt;
		rt.left = 0;
		rt.top = 0;
		rt.right = m_pNormalImage->GetWidth();
		rt.bottom = m_pNormalImage->GetHeight();
		this->Create(lpszCaption, m_buttonStyle,rt, pParentWnd,nID);
	}
}



BOOL CSimpleImageButton::OnEraseBkgnd(CDC* pDC)
{
	// TODO: Add your message handler code here and/or call default
	return TRUE;	
}


void CSimpleImageButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
	CString s;
	s.Format(L"state %d \n",m_eState );
	TRACE(s);
	CDC dc;
	dc.Attach(lpDrawItemStruct->hDC);
	CRect rcItem = lpDrawItemStruct->rcItem;
	int   nState = lpDrawItemStruct->itemState;
	
	Bitmap* image = NULL;//m_pNormalImage;
	switch (m_eState)
	{
	case enmButtonState_Normal:
		{
			image = m_pNormalImage;
		}
		break;
	case enmButtonState_Hover:
		{
			image = m_pHorverImage;
		}
		break;
	case enmButtonState_Down:
		{
			image = m_pPressImage;
		}
		break;
	}
	if (image)
	{
		Graphics g(dc.GetSafeHdc());		
		g.DrawImage(image,0,0, image->GetWidth(), image->GetHeight());
	}
	dc.Detach();
	// TODO:  Add your code to draw the specified item
}


void CSimpleImageButton::OnMouseLeave()
{
	// TODO: Add your message handler code here and/or call default
	ChangeState(enmButtonState_Normal);	
	CButton::OnMouseLeave();
}


void CSimpleImageButton::OnMouseHover(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default
	ChangeState(enmButtonState_Hover);
	//CButton::OnMouseHover(nFlags, point);
}


void CSimpleImageButton::OnLButtonDown(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default
	ChangeState(enmButtonState_Down);
	CButton::OnLButtonDown(nFlags, point);
}


void CSimpleImageButton::OnLButtonUp(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default
	ChangeState(enmButtonState_Normal);
	CButton::OnLButtonUp(nFlags, point);
	if (m_eventListener)
	{
		m_eventListener->OnClick(this);
	}
}

void CSimpleImageButton::ChangeState(ButtonMouseState eState)
{
	CString s;
	s.Format(L"Change state %d \n",eState );
	if (m_eState == eState)
	{
		return;
	}
	m_eState = eState;
	CRect rt;
	GetWindowRect(rt);
	
	
	if (NULL != GetParent())
	{
		this->GetParent()->ScreenToClient(rt);
		this->GetParent()->InvalidateRect(rt);		
	}
	//UpdateWindow();
	//Invalidate(FALSE);
}

void CSimpleImageButton::setButtonEventListener(ISimpleImageButtonEventListener* listener)
{
	m_eventListener = listener;
}

void CSimpleImageButton::OnMouseMove(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default
	TRACKMOUSEEVENT tme;
	tme.cbSize = sizeof(tme);
	tme.hwndTrack = GetSafeHwnd();
	tme.dwFlags = TME_LEAVE|TME_HOVER;
	tme.dwHoverTime = 1;	
	TrackMouseEvent(&tme);
	CButton::OnMouseMove(nFlags, point);
}
